package cn.gson.crm.controller.finance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.FollowUpDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Emp;
import cn.gson.crm.model.entity.FollowUp;
import cn.gson.crm.model.entity.Grade;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.entity.User;
import cn.gson.crm.model.mapper.FollowUpMapper;
import lombok.extern.log4j.Log4j;

/**
 * 跟进记录控制器
 * @author Jkelove
 *
 */
@Controller
@RequestMapping("/followup")
@Log4j
public class FollowUpController {
	
	@Autowired
	private FollowUpMapper followmapper;
	
	@Autowired
	private FollowUpDao followDao;
	/**
	 * 查询签约记录
	 * @param student
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> index(HttpSession session,FollowUp follow,@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		//获取登录用户session
		User user = (User) session.getAttribute("user");
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<FollowUp> pageData = new ArrayList<>();

		if("部门经理".equals(user.getEmp().getRemark())) {
			pageData = followmapper.followlist(follow);
		}else {
			follow.setEmp(user.getEmp());
			pageData = followmapper.followemplist(follow);
		}
		PageInfo<FollowUp> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	/**
	 * 删除跟进记录
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		FollowUp f;
		if (id != null && (f = followDao.findOne(id)) != null) {
			f.setDelete(false);
			obj.put("success", true);
		} else {
			obj.put("message", "此记录不存在！");
		}
		return obj;
	}
	
	/**
	 * 打开批阅界面
	 * @param fu
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping({"/piyue","/piyue/{id}"})
	public String piyue(FollowUp fu,@PathVariable(name = "id",required = false)Long id,Model model) {
		if(id != null) {
			fu = followDao.findOne(id);
			log.info("fu:"+fu);
			if(fu != null) {
				model.addAttribute("fu", fu);
			}
		}
		return "/followup/piyue";
	}
	/**
	 * 保存批阅内容
	 * @param follow
	 * @param br
	 * @return
	 */
	@PostMapping("/piyuesave")
	@ResponseBody
	@Transactional
	public Map<String, Object> follow_upsave(@Valid FollowUp follow,BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
				follow.setRemark(follow.getRemark());
				follow.setDelete(true);
				followDao.save(follow);
				obj.put("success", true);
		}
		return obj;
	}
}
